package com.ddf.publics.service.impl;

import com.ddf.publics.dao.LoginDao;
import com.ddf.publics.dao.UserDao;
import com.ddf.publics.entity.User;
import com.ddf.publics.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        if (s != null && s.length() != 0) {
            User user = this.loginDao.queryByPhone(s);
            if (user != null){
                return user;
            }
            user = this.loginDao.queryByMail(s);
            if (user != null) {
                return user;
            }
        }
        throw new UsernameNotFoundException("用户不存在");
    }

    @Override
    public User queryByPhone(String phone) {
        if (phone == null || "".equals(phone)) {
            return null;
        }
        return this.loginDao.queryByPhone(phone);
    }

    @Override
    public User queryByMail(String mail) {
        return this.loginDao.queryByMail(mail);
    }
}
